﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Echinus
{
    class Machine
    {
        public Machine(string ip, string user, string pwd)  //远程
        {
            this.ip = ip;
            this.wmiuser = user;
            this.wmipwd = pwd;
            cpuinfo = new CPUInfo(ip, user, pwd);
            osinfo = new OSInfo(ip, user, pwd);
        }

        public Machine(string ip)    //本地
        {
            this.ip = ip;
            cpuinfo = new CPUInfo(ip);
            osinfo = new OSInfo(ip);
        }

        public Machine()
        {
        }

        public void Save(System.Data.Common.DbConnection conn, System.Data.Common.DbCommand commd )
        {
            
            //保存ip,wmiuser,wmipwd

            try
            {
                commd.Connection = conn;
                commd.CommandText = "SELECT COUNT(*) FROM basicinfo WHERE ip='" + ip + "'";
                conn.Open();

                System.Data.Common.DbDataReader dr = commd.ExecuteReader();
                dr.Read();
                int hasCount = Convert.ToInt32(dr[0]);
                dr.Close();

                if (hasCount > 0)
                {

                    //根据IP更新数据库
                    commd.CommandText = "UPDATE basicinfo SET wmiuser='" + wmiuser + "',wmipwd='" + wmipwd + "' WHERE ip='" + ip + "'";
                    commd.ExecuteNonQuery();
                }
                else
                {
                    //使用Insert 插入数据
                    commd.CommandText = "INSERT INTO basicinfo(ip,wmiuser,wmipwd) VALUES('" +
                        ip + "','" + wmiuser + "','" + wmipwd + "')";
                    commd.ExecuteNonQuery();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                if (conn.State != System.Data.ConnectionState.Closed)
                {
                    conn.Close();
                }
            }

            cpuinfo.Save(conn,commd);
            osinfo.Save(conn,commd);

        }

        //public Dictionary<string,List<string>> GetInfo()
        //{
        //    Dictionary<string, List<string>> infoDict = new Dictionary<string, List<string>>();
        //    foreach (MachineInfo info in infoList)
        //    {
        //        string[] temp = info.GetType().ToString().Split('.');
        //        string type = temp[temp.Length - 1];
        //        infoDict.Add(type, info.GetInfo());
        //    }
        //    return infoDict;
        //}

        //public List<string> GetInfo(string infoName)
        //{
        //    foreach (MachineInfo info in infoList)
        //    {
        //        string[] temp = info.GetType().ToString().Split('.');
        //        string type = temp[temp.Length-1];
        //        if ( type == infoName)
        //        {
        //            return info.GetInfo();
        //        }
        //    }
        //    return null;
        //}

        //public List<MachineInfo> infoList
        //{ get; set; }

        public void GetInfo(bool refresh)
        {
            if (refresh)
            {
                cpuinfo.GetInfo(true);
                osinfo.GetInfo(true);
            }
            else
            {
                cpuinfo.GetInfo(false);
                osinfo.GetInfo(false);
            }
        }

        public CPUInfo cpuinfo{get;set;}

        public OSInfo osinfo{get;set;}

        public string ip{get;set;}
        
        public string wmiuser{get;set;}
        public string wmipwd { get; set; }
    }
}
